package com.hmdp.core.service.impl;

import com.hmdp.entity.db.ShopType;
import com.hmdp.core.mapper.ShopTypeMapper;
import com.hmdp.core.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.common.constant.RedisConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Slf4j
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Autowired
    private RedisServiceImpl redisService;

    @Override
    public List<ShopType> listAll() {
        List<ShopType> result = redisService.getList(RedisConstants.CACHE_SHOP_TYPE_KEY, ShopType.class);
        if(result.size() != 0){
            return result;
        }

        result = lambdaQuery().orderByAsc(ShopType::getSort).list();
        if(!Objects.isNull(result)){
            redisService.set(RedisConstants.CACHE_SHOP_TYPE_KEY,result);
        }
        return result;
    }
}
